package com.DesignPattern;

import com.DesignPattern.Annotations.Aspect;
import com.DesignPattern.bean.Student;
import com.DesignPattern.bean.SuperMan;
import com.DesignPattern.proxy.MyProxy;

import javax.security.auth.Subject;
import java.util.Map;

/**
 * @Author you-name
 * @Date 2022/4/11
 */
public class AOP {
    public static void main(String[] args) {
        String completeClassName = "com.DesignPattern.bean.Student";
        Object bean = IOC.getBean(completeClassName);
        SuperMan persion = (SuperMan) bean;
        persion.add(1, 1);
    }

    static {
        init();
    }

    private static void init() {
        updateBeanFromBeanFactory();
    }

    /**
     * 扫描 BeanFactory，找出类上有 @Aspect 注解的 bean，为其创建代理类，并取代原来的bean
     */
    private static void updateBeanFromBeanFactory() {
        for (Map.Entry<String, Object> entry : IOC.getBeanFactory().entrySet()) {
            if (null != entry.getValue().getClass().getDeclaredAnnotation(Aspect.class)) {
                MyProxy.updateBean(entry.getKey(), entry.getValue());
            }
        }
    }
}
